package com.wedo.mall.activity.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import com.e.library.activity.EActivity;
import com.wedo.mall.R;
import com.wedo.mall.fragment.SearchAgentFragment;
import com.wedo.mall.fragment.SearchGoodsFragment;
import com.wedo.mall.widget.SearchView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索
 * Created by lxy on 2017/6/20.
 */

public class SearchActivity extends EActivity {

    @BindView(R.id.btn_goods)   Button btn_goods;
    @BindView(R.id.v_goods)     View v_goods;
    @BindView(R.id.btn_shop)    Button btn_shop;
    @BindView(R.id.v_shop)      View v_shop;
    @BindView(R.id.sv_search) SearchView sv_search;

    private SearchGoodsFragment mSearchGoods = SearchGoodsFragment.with();
    private SearchAgentFragment mSearchAgent = SearchAgentFragment.with();

    public static void toSearchGoods(EActivity activity){
        toSearch(activity,0);
    }

    public static void toSearchAgent(EActivity activity){
        toSearch(activity,1);
    }

    public static void toSearch(EActivity activity,int endpoint){
        Bundle bundle = new Bundle();
        bundle.putInt("endpoint",endpoint);
        activity.toActivity(SearchActivity.class,bundle);
    }

    @Override
    protected int layout() {
        return R.layout.activity_search;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

        int endpoint = getIntent().getIntExtra("endpoint",0);
        if(endpoint == 0) {
            onGoodsClick();
        }else {
            onShopClick();
        }

        sv_search.listener(new SearchView.SearchListener() {
            @Override
            public void search(String keyword) {
                SearchActivity.this.search(keyword);
            }
        });
        sv_search.requestPermission(this);
    }

    @OnClick(R.id.btn_goods)
    protected void onGoodsClick(){
        btn_goods.setSelected(true);
        v_goods.setVisibility(View.VISIBLE);
        btn_shop.setSelected(false);
        v_shop.setVisibility(View.INVISIBLE);
        showFragment(mSearchGoods);
    }

    @OnClick(R.id.btn_shop)
    protected void onShopClick(){
        btn_goods.setSelected(false);
        v_goods.setVisibility(View.INVISIBLE);
        btn_shop.setSelected(true);
        v_shop.setVisibility(View.VISIBLE);
        showFragment(mSearchAgent);
    }

    /**搜索*/
    private void search(String keyword){
        hideKeybord();
        if (mSearchGoods.isAdded() && !mSearchGoods.isHidden()){
            mSearchGoods.search(keyword);
        }else if (mSearchAgent.isAdded() && !mSearchAgent.isHidden()){
            mSearchAgent.search(keyword);
        }
    }

    private void showFragment(Fragment fragment) {

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction tr = fm.beginTransaction();
        Fragment f = fm.findFragmentById(R.id.fl_fragment);
        if (f !=null && !f.isHidden()){
            tr.hide(f);
        }
        if (fragment.isAdded()) {
            tr.show(fragment).commitAllowingStateLoss();
        } else {
            tr.add(R.id.fl_fragment, fragment).show(fragment).commitAllowingStateLoss();
        }

        sv_search.hideHistory();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        sv_search.onRequestPermissionsResult(this,requestCode,permissions,grantResults);
    }
}
